<cfcomponent extends="Controller">
	<cffunction name="discharge">
		<cfscript>
			var documentID = 12;
			document = getDischargeDocument(documentID);
			
			if(document.raceID IS "") document.raceID = 0;
		</cfscript>
	</cffunction>
	
	<cffunction name="getDischargeDocument">
		<cfargument name="documentID" default="0">
		<cfscript>
			consumer = Model("consumer").findByKey(key=PARAMS.key, returnAs="query");
			dischargeDocuments = Model("document").findAll(include="discharge", where="id=#ARGUMENTS.documentID# AND consumerID=#PARAMS.key#");
		</cfscript>
		<cfquery name="document" dbtype="query">
			SELECT 	*, (consumer.firstName + ' ' + consumer.lastName) [consumerName]
			FROM 	dischargeDocuments, consumer 
			WHERE 	dischargeDocuments.consumerID = consumer.id
		</cfquery>
		<cfreturn document>
	</cffunction>

	<cffunction name="getServices">
		<cfscript>
			var services = model("dischargeInformation_services").findAll(where="consumerID=#PARAMS.key#");
			$services = [];
			serviceLabels = [
					"Custodial Agents","Psychiatric Emergency Facilities"," Psychiatric Inpatient Facilities","Psychiatric Rehabilitation Programs","Outpatient Mental Health Provider",
					"Somatic Care Providers","Schools","Local Department of Social Services","Local Department of Juveille Services","Local Education Authority","Local Coordinating Council",
					"Local Management Board","Criminal Justice Agencies","Other"
				];
				
			for(intRow = 1; intRow LTE ArrayLen(serviceLabels); intRow++) {
				$services[intRow] = {id=0, label=serviceLabels[intRow], house="", street = "", name="", phone="", city="", state="", zipcode="", startDate="", endDate="", time="" };
			}

			for(intRow = 1; intRow LTE services.recordCount; intRow++) {
				for(intSubRow = 1; intSubRow LTE ArrayLen($services); intSubRow++) {
					if($services[intSubRow].label IS services.serviceType) {
						for(column IN "agency,name,street,phone,city,state,zipcode,stateDate,endDate,time") {
							$services[intSubRow][column] &= services['service' & column][intRow];
						}
						$services[intRow].id += services.id[intRow];
					}
				}
			}

			return $services;
		</cfscript>
	</cffunction>
</cfcomponent>